package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import static com.sky.constant.RedisConstant.SHOP_STATUS_KEY;

@RestController
@RequestMapping("/admin/shop")
@Api(tags = "管理员设置营业状态")

public class AdminShopController {

    @Autowired
    private RedisTemplate redisTemplate;

    @PutMapping("/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result setShopStatus(@PathVariable Integer status) {
        /*我们直接操作redis里面的*/
        /*如果里面默认没有东西的话我们赋值为0,我们使用枚举*/

        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object o = valueOperations.get(SHOP_STATUS_KEY);
        if(o==null){
            valueOperations.set(SHOP_STATUS_KEY,0);
        }
        valueOperations.set(SHOP_STATUS_KEY,status);
                        return Result.success();
    }
    @GetMapping("/status")
    @ApiOperation("查看店铺营业状态")
    public Result getShopStatus(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer o = (Integer)valueOperations.get(SHOP_STATUS_KEY);
        return Result.success(o);
    }

}
